package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.mapper.SysCourseMapper;
import com.ruoyi.system.service.ISysCourseService;
import com.ruoyi.system.service.IInstitutionService;
import com.ruoyi.system.service.impl.InstitutionUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理模块 控制层
 * @author Ander.Zhao
 *
 */
@Controller
@RequestMapping("/itsystem/course")
public class SysCourseController extends BaseController {
    private String prefix = "system/course";
    @Autowired
    private ISysCourseService courseService;
    @Autowired
    private SysCourseMapper sysCourseMapper;
    @Autowired
    private IInstitutionService  institutionService;
    @Autowired
    private InstitutionUserServiceImpl institutionUserService;
    @GetMapping()
    public String course(ModelMap mmap)
    {
        Institution institution =new Institution();
        mmap.put("coursetypes", sysCourseMapper.selectAllCourseType());
        mmap.put("institutions",institutionService.selectAllInstitution(institution));
        mmap.put("grades", sysCourseMapper.selectAllGrade());
        return prefix + "/course";
    }
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectAllCourse( Course course){

        List<Course> list = courseService.selectAll(course);
            return getDataTable(list);
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult deleteCourse(String ids) {
        Long[] longs = Convert.toLongArray(ids);
        return toAjax(courseService.delect(longs));
    }

    @GetMapping("/add")
    public String add(ModelMap mmap){
        mmap.put("coursetypes", sysCourseMapper.selectAllCourseType());
        mmap.put("institutions",institutionService.selectAllInstitution(new Institution()));
        mmap.put("grades", sysCourseMapper.selectAllGrade());
        return prefix+"/add";
    }
    @GetMapping("/addUser/{id}")
    public String addUser(@PathVariable("id") Long Id,ModelMap mmap){
        Course course = courseService.selectById(Id);
        mmap.put("smallUser", courseService.selectUser(course.getItId()));
        mmap.put("course",course);
        return prefix+"/addUser";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Course course)
    {
        String[] split = course.getcName().split(",");
        course.setcName(split[0]);
        return toAjax(courseService.add(course));
    }

    /**u
     * 修改课程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long Id, ModelMap mmap)
    {
        mmap.put("institutions",institutionService.selectAllInstitution(new Institution()));
        mmap.put("grades", sysCourseMapper.selectAllGrade());
        mmap.put("coursetypes", sysCourseMapper.selectAllCourseType());
        mmap.put("course",courseService.selectById(Id));
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Course course)
    {
        String[] split = course.getcName().split(",");
        course.setcName(split[0]);
        return toAjax(courseService.update(course));
    }

    @PostMapping("/checkNameUnique")
    public String checkcNameUnique(Course course){
        List<Course> courses = sysCourseMapper.checkcNameUnique(course);
        if(courses.size()>0){
            return "0";
        }
        return "1";
    }
}
